package cn.edu.hit.education.view;


import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.edu.hit.education.action.AddCourseAction;
import cn.edu.hit.education.action.AddProcessAction;
import cn.edu.hit.education.action.AddPropertyAction;
import cn.edu.hit.education.action.SpeparateCourseAction;
import cn.edu.hit.education.action.UpdateCourseAction;
import cn.edu.hit.education.action.UpdateProcessAction;
import cn.edu.hit.education.action.UpdatePropertyAction;
import cn.edu.hit.education.contentprovider.CourseContentProvider;
import cn.edu.hit.education.labelprovider.CourseLabelProvider;
import cn.edu.hit.education.listener.CourseDragSourceListener;
import cn.edu.hit.education.model.CourseNode;
import cn.edu.hit.education.model.CourseRootNode;
import cn.edu.hit.education.model.Node;
import cn.edu.hit.education.model.ProcessNode;
import cn.edu.hit.education.model.PropertyNode;
import cn.edu.hit.education.model.ResourceNode;
import cn.edu.hit.education.pojo.Course;
import cn.edu.hit.education.pojo.Process;
import cn.edu.hit.education.pojo.Property;
import cn.edu.hit.education.service.ICourseService;
import cn.edu.hit.education.service.IProcessService;
import cn.edu.hit.education.service.IScheduleService;
import cn.edu.hit.education.service.ISemesterService;

@Component
public class CourseExploreView extends ViewPart {
	@Autowired
	IProcessService processService;
	@Autowired
	ICourseService courseService;
	@Autowired
	IScheduleService scheduleService;
	@Autowired
	ISemesterService semesterService;

	public static final String ID = "cn.edu.hit.education.view.CourseExploreView";
	final Transfer[] transfer=new Transfer[]{LocalSelectionTransfer.getTransfer()};
	final Transfer[] transfer2=new Transfer[] {TextTransfer.getInstance()};

	private TreeViewer treeViewer;
	private Tree tree;
	private Node currentNode;
	
	public CourseExploreView() {
	}
	
	public void createPartControl(Composite parent) {
		treeViewer = new TreeViewer(parent, SWT.BORDER);
		tree = treeViewer.getTree();
		
		treeViewer.addDragSupport(DND.DROP_MOVE, transfer2, new CourseDragSourceListener(treeViewer));
		
		treeViewer.setContentProvider(new CourseContentProvider());
		treeViewer.setLabelProvider(new CourseLabelProvider());
		treeViewer.setInput(CourseRootNode.getRootNode());
		
		tree.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
               TreeItem selections = (TreeItem)e.item;
               Node node = (Node)selections.getData();
               if(node instanceof CourseNode){
            	   currentNode = (CourseNode)node;
               }
            }
        });

		/*
		// 实现课程页面右键点击弹出菜单--分散授课
		Menu contextMenu = new Menu(tree);
		tree.setMenu(contextMenu);
		
		MenuItem separateItem = new MenuItem(contextMenu, SWT.None);
		separateItem.setEnabled(false);
		separateItem.setText("分散授课");
		
		separateItem.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				// TODO :弹出窗口实现
			
				Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
				SeparateCourseDialog dlg = new SeparateCourseDialog(parentShell, courseNode.getId());
				
				if(IDialogConstants.OK_ID == dlg.open()){
				}
				
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});*/
		
		makeContextMenu();
	}

	private void makeContextMenu() {
		MenuManager menuMgr = new MenuManager();

        Menu menu = menuMgr.createContextMenu(treeViewer.getControl());
        menuMgr.addMenuListener(new IMenuListener() {
            @Override
            public void menuAboutToShow(IMenuManager manager) {
                if (treeViewer.getSelection().isEmpty()) {
                    return;
                }

                if (treeViewer.getSelection() instanceof IStructuredSelection) {
                    IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
                    currentNode = (Node)selection.getFirstElement();

                    if(currentNode instanceof ResourceNode){
                    	menuMgr.add(new AddProcessAction(CourseExploreView.this));
					}else if(currentNode instanceof ProcessNode){
						menuMgr.add(new AddPropertyAction(CourseExploreView.this,currentNode.getId()));
						menuMgr.add(new UpdateProcessAction(CourseExploreView.this,currentNode.getId()));
					}else if(currentNode instanceof PropertyNode){
						menuMgr.add(new AddCourseAction(CourseExploreView.this,currentNode.getId()));
						menuMgr.add(new UpdatePropertyAction(CourseExploreView.this,currentNode.getId()));
					}else{
						menuMgr.add(new UpdateCourseAction(CourseExploreView.this,currentNode.getId()));
						menuMgr.add(new SpeparateCourseAction(CourseExploreView.this,currentNode.getId()));
					} 
                }
            }
        });
        menuMgr.setRemoveAllWhenShown(true);
        treeViewer.getControl().setMenu(menu);
        
	}
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}
	
	public void refreshNewData(Object obj){
		if(obj instanceof Process){
			Process process = (Process)obj;
			ProcessNode processNode = new ProcessNode(process.getId(),process.getName());
			currentNode.addChild(processNode);
		}else if(obj instanceof Property){
			Property property = (Property)obj;
			PropertyNode propertyNode = new PropertyNode(property.getId(),property.getName());
			currentNode.addChild(propertyNode);
		}else if(obj instanceof Course){
			Course course = (Course)obj;
			CourseNode courseNode = new CourseNode(course.getId(),course.getName());
			currentNode.addChild(courseNode);
		}else{
			
		}
		
		treeViewer.refresh(currentNode,true);
		treeViewer.setExpandedState(currentNode, true);
	}
	
	public void refreshUpdateData(Object obj){
		if(obj instanceof Process){
			Process process = (Process)obj;
			currentNode.setName(process.getName());
		}else if(obj instanceof Property){
			Property property = (Property)obj;
			currentNode.setName(property.getName());
		}else if(obj instanceof Course){
			Course course = (Course)obj;
			currentNode.setName(course.getName());
		}else{
			
		}
		
		treeViewer.refresh(currentNode,true);
		treeViewer.setExpandedState(currentNode, true);
	}
}
